//
//  ContentsTableViewController.swift
//  UI
//
//  Created by admin on 14/9/17.
//  Copyright (c) 2014年 sinosun. All rights reserved.
//

import UIKit


var name_controllers : [String] = ["View Controller","Navigation Controller","Tableview Controller", "Tab Bar Controller",
    "Split View Controller", "Page View Controller", "GLKit View Controller", "Object",
    "Collection View Controller", "AVKit Player View Controller", "Search Bar And Search Display Controller"]

var name_tools : [String] = ["Label","Button","TextField","Switch,Slider,Segmented","Stepper","Date Picker","ToolBar"]

var name_views : [String] = ["Activity indicator View","Progress View","Image View","Text View","Picker View","MapKit View","GLKit View","iAd BannerView","SceneKit View","Web View"]

var name_others : [String] = ["NetWork", "Device"]




class ContentsTableViewController: UITableViewController {
    
    var names : [[String]] = [ name_controllers, name_tools, name_views, name_others]

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.rightBarButtonItem = self.editButtonItem()


        self.title = "控件"
    }
    
    func leaveEditMode(){
        println("----------")
    }
    
    
    func enterEditMode(){
        println("============")
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return names.count
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return names[section].count
    }

    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell

        // Configure the cell...
        var text  = self.names[indexPath.section][indexPath.row]
        cell.textLabel?.text = text
        return cell
    }
    

    /*
    // Override to support conditional editing of the table view.

    */
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the specified item to be editable.
        return true
    }


    /*
    // Override to support editing the table view.
*/
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            self.names[indexPath.section].removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }


    
    // Override to support rearranging the table view.
    //对单元格进行重排序
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
        var newElement = self.names[fromIndexPath.section][fromIndexPath.row]
        self.names[fromIndexPath.section].removeAtIndex(fromIndexPath.row)
        self.names[toIndexPath.section].insert(newElement, atIndex: toIndexPath.row)
    }
    

    
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the item to be re-orderable.
        return true
    }
    
    // MARK: - UITableViewDelegate
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
        println(indexPath.row)
        performSegueWithIdentifier(self.names[indexPath.section][indexPath.row], sender: self)
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
 */
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
        var toVC = segue.destinationViewController as UIViewController
        var indexRow = self.tableView.indexPathForSelectedRow()
        var val = self.names[indexRow!.section][indexRow!.row]
        toVC.setValue(val, forKey: "title")
    }


}
